package com.kwok.ssm.springmvc.exception.resolver;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 自定义异常解析器
public class CustomExceptionResolver implements HandlerExceptionResolver {
    /**
     * 执行时刻：当handler执行过程中出现异常抛出时执行，并对不同异常进行捕获，跳转到不同页面
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof NullPointerException) {
            modelAndView.setViewName("redirect:/jsp/exception/error1.jsp");
        } else if (e instanceof ArithmeticException) {
            modelAndView.setViewName("redirect:/jsp/exception/error2.jsp");
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            modelAndView.setViewName("redirect:/jsp/exception/error3.jsp");
        } else {
            modelAndView.setViewName("redirect:/jsp/exception/error.jsp");
        }
        return modelAndView;
    }
}
